package programmercarl.二叉树.C13;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
 }
class Solution {
    private List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        forHead(root,"");
        return res;
    }

    private void forHead(TreeNode root,String path) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            res.add(new StringBuffer(path).append(root.val).toString());
            return;
        }
        String string = new StringBuilder(path).append(root.val).append("->").toString();
        forHead(root.left, string);
        forHead(root.right, string);
    }
}